function [Z,f,totalIter,errorMes] = ContinuationSolver(Z0,nx,nx1,ny,params,extraParams,hx,Xss,Yss,x_t,y_tN,N,logTransY,logTransX,...
    residualMax,tolf,MaxIter,JacobianOption,updateJDefaultOn,dispOn,setup) 
% Thus function solves the DSGE model under perfect foresight by solving a
% fixed point problem using the Continuation Method. See Judd (1998).

%% Settings
tol_s           = 0.02/10;   %Tolerance for step size
epsValue_s      = 1e-4;  %Tolerance of s deviation from 1
s_init          = 0.20;   %Initial weight in the Homotopy function (0 is the known problem at the steady state)
s_step_init     = 0.20;   %Initial step length
s_decrease      = 0.50;  %Decrease factor;
s_step          = s_step_init;
s               = s_init - s_step;
MaxIterFPsolver = 50;
iter            = 0;
totalIter       = 0;
errorMes        = 0;
while s < (1-epsValue_s) && s_step > tol_s && iter < MaxIter
    iter          = iter + 1;
    s             = min(s + s_step,1);
    x_continuation= s*x_t; 
    y_tN_homotopy = s*y_tN;
    if iter == 1
        Z_old  = Z0*s;
    end
    if dispOn == 1
        disp(['step size  = ',num2str(s)]);
        disp('');
    end    
    
    %Fixed point solution for scaled down problem
    dispOnFixed = 0;
    [Z,f,diff,iterSolver,errorMes] = FixedPointSolver(Z_old,nx,nx1,ny,params,extraParams,hx,Xss,Yss,x_continuation,y_tN_homotopy,N,logTransY,logTransX,...
        residualMax,tolf,MaxIterFPsolver,JacobianOption,updateJDefaultOn,dispOnFixed,setup);                               
    if errorMes == 1
        % We try a more powerfull fixed point solver.
        dispOnFixed = 0;
        [Z,f,diff,iterSolver,errorMes2] = FixedPointSolver_deltaOpt(Z_old,nx,nx1,ny,params,extraParams,hx,Xss,Yss,x_continuation,y_tN_homotopy,N,logTransY,logTransX,...
            residualMax,tolf,MaxIterFPsolver,JacobianOption,dispOnFixed,setup);
        if errorMes2 == 1
            s      = s - s_step;
            s_step = s_step*s_decrease; %Reducing the step length
        else 
            Z_old = Z;
        end
    else
        Z_old = Z; %Keeping the current step length 
    end    
    totalIter = totalIter + iterSolver;
end
if s_step <  tol_s || iter > MaxIter || s < 1
    errorMes = 1;
end

end